/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 *
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.invoker.processor;

import static org.junit.Assert.assertEquals;

import java.util.LinkedList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.processor.CommandChain;
import org.openinsula.vulcano.core.command.processor.CommandProcessor;
import org.openinsula.vulcano.core.command.processor.DefaultCommandChain;

public class DefaultCommandChainTestCase {

	private int before;
	
	private int after;
	
	@Before
	public void init() {
		before = 0;
		after = 0;
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testChain() {
		List<CommandProcessor> commandProcessorList = new LinkedList<CommandProcessor>();
		
		commandProcessorList.add(new CommandProcessor() {
			@Override
			public <T> T doExecute(Command<T> command, CommandChain commandChain) {
				before++;
				T result = commandChain.doChain(command);
				after++;
				return result;
			}
		});
		CommandChain commandChain = new DefaultCommandChain(commandProcessorList.iterator());

		assertEquals("a", commandChain.doChain(new Command<Object>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Object execute() {
				return "a";
			}
		}));
		assertEquals(1, before);
		assertEquals(1, after);
		
		commandProcessorList.add(new CommandProcessor() {
			@Override
			public <T> T doExecute(Command<T> command, CommandChain commandChain) {
				T result = commandChain.doChain(command);
				after++;
				return result;
			}
		});

		commandChain = new DefaultCommandChain(commandProcessorList.iterator());
		
		assertEquals("a", commandChain.doChain(new Command<Object>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Object execute() {
				return "a";
			}
		}));
		assertEquals(2, before);
		assertEquals(3, after);
	}

}
